package com.jrx.anytxn.param.service.rights.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.operation.PrPromotionInfoReq;
import com.jrx.anytxn.param.dto.operation.PrPromotionInfoRes;
import com.jrx.anytxn.param.entity.PrPromotionInfo;
import com.jrx.anytxn.param.mapper.ext.ExtPrPromotionInfoMapper;
import com.jrx.anytxn.param.service.rights.IPrPromotionInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 张靖烽
 * @name PrPromotionInfoServiceImpl
 * @description
 * @create 2020-12-08 17:41
 **/
@Service
@CacheConfig(cacheNames = "PROMOTION_INFO")
public class PrPromotionInfoServiceImpl implements IPrPromotionInfoService {

    private static final Logger logger = LoggerFactory.getLogger(PrPromotionInfoServiceImpl.class);

    @Resource
    private ExtPrPromotionInfoMapper extPrPromotionInfoMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<PrPromotionInfoRes> getAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId)
            throws TxnException {
        Page<PrPromotionInfoRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrPromotionInfo> prPromotionInfos = extPrPromotionInfoMapper.selectByTenId(tenantId);
        if (prPromotionInfos.isEmpty()) {
            logger.error("查询的促销活动信息表列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrPromotionInfoRes> res = BeanMapping.copyList(prPromotionInfos, PrPromotionInfoRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }


    @Override
    public PrPromotionInfoRes getByPromotionIdAndTenId(String promotionId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(promotionId) || StringUtils.isBlank(tenantId)) {
            logger.error("活动ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrPromotionInfo prPromotionInfo = extPrPromotionInfoMapper.selectByPromotionIdAndTenId(promotionId, tenantId);
        return BeanMapping.copy(prPromotionInfo, PrPromotionInfoRes.class);
    }


    @Override
    public PrPromotionInfoRes addPromotionInfo(PrPromotionInfoReq prPromotionInfoReq) throws TxnException {
        try {
            prPromotionInfoReq.setId(segmentService.getId(Constant.PR_PROMOTION_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }

        PrPromotionInfo prPromotionInfo = BeanMapping.copy(prPromotionInfoReq, PrPromotionInfo.class);
        /*根据索引查看是否存在此信息*/
        PrPromotionInfo promotionInfo = extPrPromotionInfoMapper.selectByPromotionIdAndTenId(prPromotionInfo.getPromotionId(), prPromotionInfo.getTenantId());
        if (promotionInfo != null) {
            logger.error("增加的促销活动表信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        prPromotionInfo.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrPromotionInfoMapper.insertSelective(prPromotionInfo);
        return BeanMapping.copy(prPromotionInfo, PrPromotionInfoRes.class);
    }

    @CacheEvict(key = "'promotionInfo_'+#prPromotionInfoReq.tenantId+'_'+#prPromotionInfoReq.promotionId")
    @Override
    public PrPromotionInfoRes modifyPromotionInfo(PrPromotionInfoReq prPromotionInfoReq) {
        PrPromotionInfo prPromotionInfo = BeanMapping.copy(prPromotionInfoReq, PrPromotionInfo.class);
        extPrPromotionInfoMapper.updateByPromotionIdAndTenId(prPromotionInfo);
        return BeanMapping.copy(prPromotionInfo, PrPromotionInfoRes.class);
    }

    @CacheEvict(key = "'promotionInfo_'+#tenantId+'_'+#promotionId")
    @Override
    public Boolean removeByPromotionIdAndTenId(String promotionId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(promotionId)) {
            logger.error("活动ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extPrPromotionInfoMapper.deleteByPromotionIdAndTenId(promotionId, tenantId);
        return a > 0;
    }

    @Cacheable(key = "'promotionInfo_'+#tenantId+'_'+#promotionId")
    @Override
    public PrPromotionInfo findByPromotionIdAndTenId(String promotionId, String tenantId) {
        return extPrPromotionInfoMapper.selectByPromotionIdAndTenId(promotionId, tenantId);
    }
}
